package com.local.carousel.controller;

import com.local.carousel.pojo.Carousel;
import com.local.carousel.pojo.CarouselDeleteRequestParam;
import com.local.carousel.pojo.CarouselListRequestParam;
import com.local.carousel.service.CarouselService;
import com.local.common.util.PageResultUtil;
import com.local.common.util.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 轮播图carousel的数据api控制器
 */

@Controller
@RequestMapping("/carousel")
public class CarouselController {

    @Autowired
    private CarouselService carouselService;

    /**
     * 查询 Carousel
     * @param requestParam
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public ResultUtil list(@RequestBody CarouselListRequestParam requestParam) {
        Integer page = Objects.nonNull(requestParam.getPage()) && requestParam.getPage() > 0 ? requestParam.getPage() : 1;
        Integer limit = Objects.nonNull(requestParam.getLimit()) && requestParam.getLimit() > 0 ? requestParam.getLimit() : 10;
        String sort = Objects.nonNull(requestParam.getSort()) && requestParam.getSort().length() > 0 ? requestParam.getSort() : "carousel_rank";
        String order = Objects.nonNull(requestParam.getOrder()) && requestParam.getOrder().length() > 0 ? requestParam.getOrder() : "asc";
        Integer isDeleted = Objects.nonNull(requestParam.getIsDeleted()) ? requestParam.getIsDeleted() : -1;
        PageResultUtil<Carousel> carouselPage = carouselService.getCarouselPage(page, limit, sort, order, isDeleted);

        return new ResultUtil(200, "", carouselPage);
    }


    /**
     * 根据id查询轮播图片数据，通过远程调用carousel_service_openfeign接口方法获取数据
     * @param id
     * @return
     */
    @GetMapping("/info/{id}")
    @ResponseBody
    public ResultUtil info(@PathVariable("id") Integer id) {
        Carousel carousel = carouselService.getById(id);
        if (Objects.isNull(carousel)) {
            return new ResultUtil(201, "数据查询失败，请稍后再试");
        } else {
            return new ResultUtil(200, "", carousel);
        }
    }


    /**
     * 新增
     * @param carousel
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public ResultUtil save(@RequestBody Carousel carousel) {
        Integer saveResult = carouselService.save(carousel);
        if (saveResult > 0) {
            return new ResultUtil(200, "新增成功", carousel);
        } else {
            return new ResultUtil(201, "新增失败");
        }
    }


    /**
     * 修改
     * @param carousel
     * @return
     */
    @PostMapping("/update")
    @ResponseBody
    public ResultUtil update(@RequestBody Carousel carousel) {
        Integer saveResult = carouselService.update(carousel);
        if (saveResult > 0) {
            return new ResultUtil(200, "修改成功");
        } else {
            return new ResultUtil(201, "修改失败");
        }
    }


    /**
     * 删除，支持批量删除
     * @param carouselDeleteRequestParam
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public ResultUtil delete(@RequestBody CarouselDeleteRequestParam carouselDeleteRequestParam) {
        Integer[] ids = carouselDeleteRequestParam.getIds();
        Integer loginUserId = carouselDeleteRequestParam.getLoginUserId();
        String time = carouselDeleteRequestParam.getTime();
        Integer result = carouselService.deleteByIds(ids, loginUserId, time);
        if (result > 0) {
            return new ResultUtil(200, "删除成功");
        } else {
            return new ResultUtil(201, "删除失败，请稍后再试");
        }
    }


    /**
     * 查询轮播图（用于网站首页）
     * @return
     */
    @GetMapping("/list/index")
    @ResponseBody
    public ResultUtil getCarouselList() {
        List<Carousel> carouselList = carouselService.getCarouselList();
        if (Objects.nonNull(carouselList)) {
            return new ResultUtil(200, "", carouselList);
        } else {
            return new ResultUtil(201, "请求失败，请稍后再试");
        }
    }


}
